package com.qingmi.farmer.ui.splash;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.UserData;
import com.qingmi.farmer.data.model.Banner;
import com.qingmi.farmer.data.model.HomeData;
import com.qingmi.farmer.data.model.UserInfo;
import com.qingmi.farmer.data.repository.data.OtherDataRepository;
import com.qingmi.farmer.data.repository.data.UserDataRepository;
import com.qingmi.farmer.databinding.ActivitySplashBinding;
import com.qingmi.farmer.ui.main.MainActivity;
import com.wjjun.base.data.preference.UserPreference;
import com.wjjun.base.ui.base.BaseActivity;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * 启动页
 */
public class SplashActivity extends BaseActivity<SplashViewModel, ActivitySplashBinding> {


    public static void start(Context context) {
        Intent intent = new Intent(context, SplashActivity.class);
        context.startActivity(intent);
    }

    @Inject
    UserData userData;

    @Inject
    UserPreference userPreference;

    @Inject
    OtherDataRepository otherDataRepository;

    private Disposable disposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot() || (getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected SplashViewModel initViewModel() {
        return new SplashViewModel();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            initHomeData();

            startTimeDown();
        }
    }

    private void initHomeData() {
        //获取首页数据,因为有缓存,所以无需其他处理
        otherDataRepository.getHomeData(new DefaultObserver<HomeData>() {
            @Override
            public void onError(MyException e) {

            }
        }, false);

        //获取首页banner,因为有缓存,所以无需其他处理
        otherDataRepository.getBanner(new DefaultObserver<List<Banner>>() {
            @Override
            public void onError(MyException e) {

            }
        }, false);

    }


    private void startTimeDown() {
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(6)//设置总共发送的次数
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) {
                        return 5 - aLong;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (disposable != null) {
                            disposable.dispose();
                        }
                        disposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        viewModel.countdown.set(aLong);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        startNextActivity();
                    }
                });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_countdown:
                startNextActivity();
                break;
        }
    }

    private void startNextActivity() {
        MainActivity.start(SplashActivity.this);
        finish();
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
    }
}
